﻿using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;

public class NetData {
    public short protocol;  // 协议
    public int length;    // 包长度
    public byte[] data;     // 包内容
    public byte md5;    // 校验

    public NetData()
    {

    }

    public NetData(short protocol, byte[] data)
    {
        this.protocol = protocol;
        this.data = data;
        this.length = data.Length;
    }

    public byte[] ToBytes()
    {
        length = 7;
        if (data != null)
            length += (short) data.Length;

        byte[] msg = new byte[length];

        byte[] sbytes = shortToByte(protocol);

        Array.Copy(sbytes, 0, msg, 0, 2);

        sbytes = intToByte(length);

        Array.Copy(sbytes, 0, msg, 2, 4);

        if (data != null)
            Array.Copy(data, 0, msg, 6, length - 7);

        msg[length - 1] = md5;

        return msg;
    }

    public int fill(byte[] msg)
    {
        if (msg.Length < 7)
            return 0;

        protocol = byteToShort(msg, 0);

        length = byteToInt(msg, 2);

        if (length > msg.Length)
            return 0;

        if (length < 7)
            return -1;

        if (length > 7)
        {
            data = new byte[length - 7];

            Array.Copy(msg, 6, data, 0, length - 7);
        }

        md5 = msg[length - 1];

        return length;
    }

    public static string byteToString(byte[] b, int offset, int size)
    {
        return System.Text.Encoding.UTF8.GetString(b, offset, size);
    }

    public static byte[] longToByte(long number)
    {
        byte[] b = BitConverter.GetBytes(number);
        return b;
    }

    //byte数组转成long 
    public static long byteToLong(byte[] b, int offset)
    {
        long s = BitConverter.ToInt64(b, offset);
        return s;
    }

    /** 
	     * 注释：int到字节数组的转换！ 
	     * 
	     * @param number 
	     * @return 
	     */
    public static byte[] intToByte(int number)
    {
        byte[] b = BitConverter.GetBytes(number);
        return b;
    }

    /** 
     * 注释：字节数组到int的转换！ 
     * 
     * @param b 
     * @return 
     */
    public static int byteToInt(byte[] b, int offset)
    {
        int s = BitConverter.ToInt32(b, offset);
        return s;
    }

    /** 
     * 注释：short到字节数组的转换！ 
     * 
     * @param s 
     * @return 
     */
    public static byte[] shortToByte(short number)
    {
        byte[] b = BitConverter.GetBytes(number);
        return b;
    }


    /** 
     * 注释：字节数组到short的转换！ 
     * 
     * @param b 
     * @return 
     */
    public static short byteToShort(byte[] b, int offset)
    {
        short s = BitConverter.ToInt16(b, offset);
        return s;
    }

    /**
     * 浮点转换成字节数组
     * @param f
     * @return
     */
    public static byte[] floatToByte(float f)
    {
        // 把float转换为byte[]
        byte[] b = BitConverter.GetBytes(f);

        return b;
    }

    /**
     * 字节数组到float的转换
     * @param b
     * @param offset
     * @return
     */
    public static float byteToFloat(byte[] b, int offset)
    {
        return BitConverter.ToSingle(b, offset);
    }
}
